//
//  WindowTopLogView.swift
//  ltsp
//
//  Created by jyvyi_ios on 2022/7/11.
//

import UIKit

class WindowLogDataSource{
    
    static var shared = WindowLogDataSource()
    
    weak var collectionView : UICollectionView?
    
    var logs : [String] = []
    
    static func log(_ text:String){
        
        self.shared.logs.insert(text, at: 0)
        self.shared.collectionView?.reloadData()
    }
    
    static func clearLog(){
        
        self.shared.logs.removeAll()
        self.shared.collectionView?.reloadData()
    }
}

class WindowTopLogView: PYOverUserInteractionEnableView{
    
    var isClosed : Bool = true{
        didSet{
            
            self.logView.isHidden = isClosed
            self.showBtn.isHidden = !isClosed
        }
    }
    
    init(){
        super.init(frame: .zero)
        self.initializeSubView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initializeSubView(){
        
        self.addSubview(self.logView)
        
        self.addSubview(self.showBtn)
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.logView.frame = self.bounds
    }
    //MARK: EVENT
    @objc func showLogViewEvent(){
        
        self.isClosed = false
    }
    
    //MARK:  LAZY
    lazy var logView: WindowLogView = {
        
        let view = WindowLogView()
        view.closeBlock = {[weak self] in
            guard let weakSelf = self else{
                return
            }
            weakSelf.isClosed = true
        }
        return view
    }()
    
    lazy var showBtn: ShowLogButton = {

        let btn = ShowLogButton.init(frame: CGRect.init(x: 0, y: ScreenHeight / 2, width: 40.W, height: 40.W))
        btn.addTarget(self, action: #selector(showLogViewEvent), for: .touchUpInside)
        return btn
    }()
}

class WindowLogView : UIView{
    
    var closeBlock : EmptyCompleteBlock?
    
    init(){
        super.init(frame: .zero)
        self.isHidden = true
        
        self.backgroundColor = "000000".HexColor(alpha: 0.8)
        self.initializeSubView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initializeSubView(){
        
        self.addSubview(self.collectionView)
        self.collectionView.snp.makeConstraints { make in
            make.top.equalTo(self.safeAreaLayoutGuide_PY)
            make.left.right.equalToSuperview()
            make.height.equalTo(500.H)
        }
        
        self.addSubview(self.closeBtn)
        self.closeBtn.snp.makeConstraints { make in
            make.centerX.equalTo(self.snp.centerX).offset(-100)
            make.top.equalTo(self.collectionView.snp.bottom).offset(40)
            make.size.equalTo(CGSize.init(width: 50, height: 20))
        }
        
        self.addSubview(self.clearBtn)
        self.clearBtn.snp.makeConstraints { make in
            make.centerX.equalTo(self.snp.centerX).offset(100)
            make.centerY.equalTo(self.closeBtn)
            make.size.equalTo(CGSize.init(width: 50, height: 20))
        }
        
        self.addSubview(self.changeHostBtn)
        self.changeHostBtn.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(self.closeBtn.snp.bottom).offset(40)
            make.size.equalTo(CGSize.init(width: 100, height: 20))
        }
    }
    @objc func closeLogViewEvent(){
        
        self.closeBlock?()
    }
    @objc func changeHostEvent(){
        
        self.closeLogViewEvent()
        RequestHostManager.changeHost()
    }
    @objc func clearLogViewEvent(){
        
        WindowLogDataSource.clearLog()
    }
    //MARK: LAZY
    lazy var collectionView: UICollectionView = {
        
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .vertical
        layout.minimumLineSpacing = 10
        layout.minimumInteritemSpacing = 0
        
        let view = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        view.backgroundColor = UIColor.clear
        view.dataSource = self
        view.delegate = self
        
        WindowLogDataSource.shared.collectionView = view
        
        view.register_PY(WindowLogCollectionViewCell.self)
        
        return view
    }()
    lazy var closeBtn: UIButton = {
        
        let btn = UIButton.init(type: .custom)
        btn.setTitle("关闭", for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        btn.setTitleColor(UIColor.white, for: .normal)
        btn.addTarget(self, action: #selector(closeLogViewEvent), for: .touchUpInside)
        
        return btn
    }()
    lazy var changeHostBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("更换HOST", for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        btn.setTitleColor(UIColor.white, for: .normal)
        btn.addTarget(self, action: #selector(changeHostEvent), for: .touchUpInside)
        return btn
    }()
    lazy var clearBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("清除", for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        btn.setTitleColor(UIColor.white, for: .normal)
        btn.addTarget(self, action: #selector(clearLogViewEvent), for: .touchUpInside)
        
        return btn
    }()
}
extension WindowLogView : UICollectionViewDelegateFlowLayout{
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        var height : CGFloat = 0
        if WindowLogDataSource.shared.logs.count > indexPath.row{
            let logString = WindowLogDataSource.shared.logs[indexPath.row]
            height = logString.height(font: UIFont.systemFont(ofSize: 12, weight: .regular), width: ScreenWidth)
        }
        return CGSize.init(width: ScreenWidth, height: height)
    }
}
extension WindowLogView : UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return WindowLogDataSource.shared.logs.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell_PY(with: WindowLogCollectionViewCell.self, for: indexPath)
        if WindowLogDataSource.shared.logs.count > indexPath.row{
            cell.logString = WindowLogDataSource.shared.logs[indexPath.row]
        }
        return cell
    }
}
class WindowLogCollectionViewCell : UICollectionViewCell{
    
    var logString : String?{
        didSet{
            self.contentLabel.text = logString
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        self.contentView.addSubview(self.contentLabel)
        self.contentLabel.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var contentLabel: UILabel = {
        
        let label = UILabel()
        label.textColor = UIColor.white
        label.numberOfLines = 0
        label.sizeToFit()
        label.font = UIFont.systemFont(ofSize: 12, weight: .regular)
        label.isUserInteractionEnabled = true
        
        let copyTap = UILongPressGestureRecognizer.init(target: self, action: #selector(copyTextEvent))
        label.addGestureRecognizer(copyTap)
        
        return label
    }()
    
    @objc func copyTextEvent(){
        
        UIPasteboard.general.string = self.logString
        PYToast.toast("复制成功", toView: self)
    }
}
class ShowLogButton : PYPanControl{
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let titleLabel = UILabel()
        titleLabel.backgroundColor = UIColor.green
        titleLabel.text = "LOG"
        titleLabel.textAlignment = .center
        titleLabel.textColor = UIColor.white
        titleLabel.frame = CGRect.init(x: 0, y: 0, width: frame.width, height: frame.height)
        self.addSubview(titleLabel)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
